from PySide6.QtWidgets import (
    QApplication,
    QMainWindow,
    QDialog,
    QFrame,
    QWidget,
    QVBoxLayout,
    QHBoxLayout,
    QLabel,
    QPushButton,
    QToolBar,
    QStyle,
    QStyleOption,
    QStackedWidget,
    QMessageBox,
    QProgressBar,
    QCommonStyle,
    QToolButton,
    QStyleOptionToolButton,
    QListWidget,
    QLCDNumber,
)
from PySide6.QtGui import (
    QIcon,
    QPixmap,
    QAction,
    QCursor,
    QPaintEvent,
    QPainter,
    QColor,
    QPainterPath,
)
from PySide6.QtCore import Qt, QSize, QTimer, Signal, QEvent
import json, requests
import image


class MainChangeWindow(QStackedWidget):
    """默认布局"""

    def __init__(self, parent=None):
        super(MainChangeWindow, self).__init__(parent)
        # self.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.firstWidget()
        self.secondWidget()
        self.setCurrentIndex(0)

    def firstWidget(self):
        self.page_one = ScanQrcodeLogin()
        self.addWidget(self.page_one)

    def secondWidget(self):
        self.page_two = fileListViews()
        self.addWidget(self.page_two)


class ScanQrcodeLogin(QWidget):
    """扫码登录"""

    def __init__(self, parent=None):
        super(ScanQrcodeLogin, self).__init__(parent)
        # self.setStyleSheet("background-color: rgb(255, 255, 255);")
        # 第一行
        title = QLabel("扫码登录")
        title.setStyleSheet("font-size: 24px;color: #ffffff;")
        h0 = QHBoxLayout()
        h0.setContentsMargins(0, 10, 0, 10)
        h0.addStretch(1)
        h0.addWidget(title)
        h0.addStretch(1)
        # 第二行
        self.code = QLabel()
        self.code.setStyleSheet("border: 1px solid #333333;")
        h1 = QHBoxLayout()
        h1.setContentsMargins(30, 10, 30, 10)
        h1.addStretch(1)
        h1.addWidget(self.code)
        h1.addStretch(1)

        # 第三行
        self.prog = QProgressBar()
        self.prog.setRange(0, 0)
        self.prog.setTextVisible(False)
        self.prog.setFixedHeight(2)
        self.prog.setStyleSheet("height: 2px;border-radius:1px;")

        h2 = QHBoxLayout()
        h2.setContentsMargins(10, 5, 10, 5)
        h2.addWidget(self.prog)
        # 第三行
        self.wechat = QLabel()
        self.wechat.setPixmap(QPixmap(":icon/wx.png").scaled(32, 32))
        h3 = QHBoxLayout()
        h3.setContentsMargins(0, 10, 0, 10)
        h3.addStretch(1)
        h3.addWidget(self.wechat)
        h3.addStretch(1)
        # 加行
        info = QLabel("请关注公众号\n个人中心 -> 我的剪辑\n")
        info.setStyleSheet("font-size: 12px;color: #555555;")
        h4 = QHBoxLayout()
        h4.setContentsMargins(10, 0, 10, 0)
        h4.addStretch(1)
        h4.addWidget(info)
        h4.addStretch(1)
        # 顶层
        v = QVBoxLayout(self)
        v.setContentsMargins(0, 0, 0, 0)
        v.addLayout(h0)
        v.addStretch(1)
        v.addLayout(h1)
        v.addStretch(1)
        v.addLayout(h4)
        v.addLayout(h2)
        v.addLayout(h3)


class fileListViews(QWidget):
    """文件列表"""

    def __init__(self, parent=None):
        super(fileListViews, self).__init__(parent)
        # self.setStyleSheet("background-color: rgb(255, 255, 255);")
        # 第一行
        self.myface = QLabel()
        self.myface.setPixmap(QPixmap("wx.png").scaled(48, 48))
        self.myface.setStyleSheet(
            "border-radius: 34px; background-color: rgb(255, 255, 255); padding: 10px;"
        )
        self.nicename = QLabel("昵称")
        self.nicename.setStyleSheet(
            "font-size: 24px;color: #ffffff; padding-left: 10px;"
        )
        h0 = QHBoxLayout()
        h0.setContentsMargins(10, 0, 10, 0)
        h0.addWidget(self.myface)
        h0.addWidget(self.nicename)
        h0.addStretch(1)

        self.panel = modelActionViews()
        self.model = modelActionList()
        h1 = QHBoxLayout()
        h1.setContentsMargins(10, 0, 10, 10)
        h1.setSpacing(0)
        h1.addWidget(self.panel)
        h1.addWidget(self.model)

        # 顶层
        v = QVBoxLayout(self)
        v.setContentsMargins(0, 0, 0, 0)
        v.addLayout(h0)
        v.addLayout(h1)
        # v.addStretch(1)

    def get_image_content(self, url: str) -> QPixmap:
        image = QPixmap()
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0"
        }
        try:
            response = requests.get(url, headers=headers)
            image.loadFromData(response.content)
            image = image.scaled(48, 48)
            return image
        except Exception as e:
            return image

    def load(self, userinfo):
        """加载用户信息"""
        if userinfo.get("nickname"):
            self.nicename.setText(userinfo.get("nickname"))
        if userinfo.get("headimgurl"):
            self.myface.setPixmap(self.get_image_content(userinfo.get("headimgurl")))


class modelActionList(QToolBar):
    """指令按钮列表"""

    def __init__(self, parent=None):
        super(modelActionList, self).__init__(parent)
        # self.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.setIconSize(QSize(18, 18))
        self.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
        self.setOrientation(Qt.Vertical)
        self.remote = QAction(
            QIcon("yun.png"),
            "远程",
            self,
        )
        self.addAction(self.remote)
        self.local = QAction(
            QIcon("bd.png"),
            "本地",
            self,
        )
        self.addAction(self.local)
        self.addSeparator()
        self.recv = QAction(
            QIcon("rec.png"),
            "录屏",
            self,
        )
        self.addAction(self.recv)
        self.remote.setCheckable(True)
        self.local.setCheckable(True)

        self.remote.setChecked(True)


class modelActionViews(QStackedWidget):
    def __init__(self, parent=None):
        super(modelActionViews, self).__init__(parent)
        self.setStyleSheet(
            "background-color: rgb(255, 255, 255); border-radius: 5px 5px;"
        )
        self.firstWidget()
        self.secondWidget()
        self.setCurrentIndex(0)

    def firstWidget(self):
        self.page_one = remoteFileListViews()
        self.addWidget(self.page_one)

    def secondWidget(self):
        self.page_two = localFileListViews()
        self.addWidget(self.page_two)


class remoteFileListViews(QWidget):
    """远程文件列表"""

    def __init__(self, parent=None):
        super(remoteFileListViews, self).__init__(parent)


class localFileListViews(QWidget):
    """本地文件列表"""

    def __init__(self, parent=None):
        super(localFileListViews, self).__init__(parent)
        self.viewlist = QListWidget(self)

    def load(self):
        self.viewlist.clear()
